package study.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.reggie.common.CustomException;
import study.reggie.dao.SetmealDao;
import study.reggie.dto.DishDto;
import study.reggie.dto.SetmealDto;
import study.reggie.pojo.Dish;
import study.reggie.pojo.DishFlavor;
import study.reggie.pojo.Setmeal;
import study.reggie.pojo.SetmealDish;
import study.reggie.service.SetmealDishService;
import study.reggie.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;




    /*
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes=setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());


        setmealDishService.saveBatch(setmealDishes);

    }


    /*
     * 根据id查询套餐信息和对应的菜品信息
     * */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //查询菜品基本信息，从dish表查询
        Setmeal setmeal=this.getById(id);

        //拷贝
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的菜品信息，从setmeal_dish表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }


    /*
    * 根据id修改套餐信息和对应的菜品信息
    */
    @Override
    @Transactional
    public void updateByIdWithDish(SetmealDto setmealDto) {
        //更新dish表基本信息
        this.updateById(setmealDto);

        //清除当前套餐对应菜品数据——setmeal_dish表的delete操作
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的菜品数据——setmeal_dish表的add操作
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        //封装菜品对应的套餐id
        dishes=dishes.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(dishes);
    }


    /*
     * 删除套餐，同时删除套餐和菜品的关联数据
     * */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);

        if(count>0){
            //如果不能删除抛出一个异常
            throw new CustomException("勾选的套餐中含有正在售卖的套餐，请先停售再删除");
        }

        //如果可以删除，先删除套餐表中的数据
        this.removeByIds(ids);

        //查询出setmeal_dish表中的关联数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据--setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);

    }
}
